
**** Figure H.14: Timing panel 

use "$datadir\ewallet\merchants_reshape_week.dta" , clear 
	
	*** Keep only valid pincodes (6 digits)
	tostring pincode, gen(pincode_str)
	g x = length(pincode_str)
	keep if x == 6 
	drop x pincode_str
	
	** Week 28 is the week of the shock
	g onplatform = transaction > 0
	
	egen merchant_id = group(merchantid)
	egen ziptime = group(pincode time)
	egen industrytime = group(industryid time)
	egen zip = group(pincode)
	
	
	bys industryid zip time : egen amount_jt = total(amount) 
	bys industryid zip time : egen transaction_jt = total(transaction) 
	bys industryid zip time : egen onplatform_jt = total(onplatform) 
	
	xtset merchant_id time 
	
	foreach var in amount transaction onplatform{
		g lag_`var'_jt = l.`var'_jt
		g lag_`var' = l.`var'
		g lag_`var'_jt_1 = lag_`var'_jt - lag_`var'
		
		g ln_lag_`var' = ln(1+lag_`var')
		g ln_lag_`var'_jt_1 = ln(1+lag_`var'_jt_1)
		g ln_`var' = ln(1+`var')
	}
	
	
	foreach var in ln_lag_amount  ln_lag_amount_jt_1 ln_amount ln_lag_transaction  ln_lag_transaction_jt_1 ln_transaction {
	bys time: egen m_`var'=mean(`var')
	bys time: egen sd_`var'=sd(`var')
	gen `var'_z=(`var'-m_`var')/sd_`var'
	drop m_`var' sd_`var'
	sum `var'_z
	}

	rename ln_lag_amount_jt_1_z ln_lag_amount_z_jt_1
	rename ln_lag_transaction_jt_1_z  ln_lag_transaction_z_jt_1

	*Monthly interaction
	tab timemonth, gen(month_id)
	foreach num of numlist 1(1)14 {
		foreach var in amount transaction onplatform{
			gen int_month`num'_lag_`var'=ln_lag_`var'_jt_1*month_id`num'
		}
	}
	
	
	foreach var in  amount transaction {
	statsby eff_1=_b[int_month1_lag_`var'] eff_2=_b[int_month2_lag_`var'] eff_3=_b[int_month3_lag_`var'] eff_4=_b[int_month4_lag_`var'] eff_5=_b[int_month5_lag_`var'] eff_6=_b[int_month6_lag_`var'] eff_7=_b[int_month7_lag_`var'] eff_8=_b[int_month8_lag_`var'] eff_9=_b[int_month9_lag_`var'] eff_10=_b[int_month10_lag_`var'] eff_11=_b[int_month11_lag_`var'] eff_12=_b[int_month12_lag_`var'] eff_13=_b[int_month13_lag_`var'] eff_14=_b[int_month14_lag_`var'] se_1=_se[int_month1_lag_`var'] se_2=_se[int_month2_lag_`var'] se_3=_se[int_month3_lag_`var'] se_4=_se[int_month4_lag_`var'] se_5=_se[int_month5_lag_`var'] se_6=_se[int_month6_lag_`var'] se_7=_se[int_month7_lag_`var'] se_8=_se[int_month8_lag_`var'] se_9=_se[int_month9_lag_`var'] se_10=_se[int_month10_lag_`var'] se_11=_se[int_month11_lag_`var'] se_12=_se[int_month12_lag_`var'] se_13=_se[int_month13_lag_`var'] se_14=_se[int_month14_lag_`var'] , saving("$datadir\time_spill`var'", replace): reghdfe ln_`var' ln_lag_`var'  int_month1_lag_`var' int_month2_lag_`var' int_month3_lag_`var' int_month4_lag_`var' int_month5_lag_`var' int_month6_lag_`var' int_month7_lag_`var' int_month8_lag_`var' int_month9_lag_`var' int_month10_lag_`var' int_month11_lag_`var' int_month12_lag_`var' int_month13_lag_`var' int_month14_lag_`var' , a(merchant_id ziptime industrytime) cluster(zip)
	
	preserve
	use "$datadir\time_spill`var'", clear
    gen i=1
    reshape long eff_ se_ , i(i) j(period)
    drop i
    rename eff_ eff
    rename se_ se
    tset period
    gen t_10=1.65
    gen t_5=1.96
  
    foreach num in 5 10 {
    gen low_bound_`num'=eff-(t_`num'*se)
    gen up_bound_`num'=eff+(t_`num'*se)
    }
	save  "$datadir\time_spill`var'_final.dta", replace
	erase "$datadir\time_spill`var'.dta"
	restore
	}
	
	 *** On platform is separate
	foreach var in onplatform {
	statsby eff_1=_b[int_month1_lag_`var'] eff_2=_b[int_month2_lag_`var'] eff_3=_b[int_month3_lag_`var'] eff_4=_b[int_month4_lag_`var'] eff_5=_b[int_month5_lag_`var'] eff_6=_b[int_month6_lag_`var'] eff_7=_b[int_month7_lag_`var'] eff_8=_b[int_month8_lag_`var'] eff_9=_b[int_month9_lag_`var'] eff_10=_b[int_month10_lag_`var'] eff_11=_b[int_month11_lag_`var'] eff_12=_b[int_month12_lag_`var'] eff_13=_b[int_month13_lag_`var'] eff_14=_b[int_month14_lag_`var'] se_1=_se[int_month1_lag_`var'] se_2=_se[int_month2_lag_`var'] se_3=_se[int_month3_lag_`var'] se_4=_se[int_month4_lag_`var'] se_5=_se[int_month5_lag_`var'] se_6=_se[int_month6_lag_`var'] se_7=_se[int_month7_lag_`var'] se_8=_se[int_month8_lag_`var'] se_9=_se[int_month9_lag_`var'] se_10=_se[int_month10_lag_`var'] se_11=_se[int_month11_lag_`var'] se_12=_se[int_month12_lag_`var'] se_13=_se[int_month13_lag_`var'] se_14=_se[int_month14_lag_`var'] , saving("$datadir\time_spill`var'", replace): reghdfe `var' lag_`var'  int_month1_lag_`var' int_month2_lag_`var' int_month3_lag_`var' int_month4_lag_`var' int_month5_lag_`var' int_month6_lag_`var' int_month7_lag_`var' int_month8_lag_`var' int_month9_lag_`var' int_month10_lag_`var' int_month11_lag_`var' int_month12_lag_`var' int_month13_lag_`var' int_month14_lag_`var' , a(merchant_id ziptime industrytime) cluster(zip)
	
	preserve
	use "$datadir\time_spill`var'", clear
		gen i=1
		reshape long eff_ se_ , i(i) j(period)
		drop i
		rename eff_ eff
		rename se_ se
		tset period
		gen t_10=1.65
		gen t_5=1.96
	  
		foreach num in 5 10 {
		gen low_bound_`num'=eff-(t_`num'*se)
		gen up_bound_`num'=eff+(t_`num'*se)
		}
	save  "$datadir\time_spill`var'_final.dta", replace
	erase "$datadir\time_spill`var'.dta"
	restore
	}
	
	
	
foreach var in amount transaction onplatform {
use "$datadir\time_spill`var'_final.dta", clear
	  gen temp="`var'"
	  local name="`var'"
		if temp=="amount" {
			local name= "amount transacted"
			local i = 1
		}
		if temp=="transaction" {
			local name= "# of transactions"
			local i = 2
		}
		if temp=="onplatform" {
			local name= "# of firms"
			local i = 3
		}
		
	replace period = period-6
	twoway (rcap low_bound_5 up_bound_5 period, lcolor(gray) lstyle(ci)) (scatter eff period, lcolor(black) mcolor(black) msymbol(circle) msize(medium)) , ///
	legend(off) graphregion(color(white)) xline(2, lwidth(26) lc(gs12)) ylab(,nogrid) xlab(-5(1)8) ylab(0(0.02)0.06) yline(0, lcolor(gs10) lpattern(dash)) ///
	xtitle("Time") ytitle("Effect on `var'") xline(0.5, lcolor(red) lpattern(dash))
    graph export "$outputdir_fig/Figure_H_14_`i'.pdf", as(pdf) replace
	 
	}

	
foreach var in amount transaction onplatform {
	erase "$datadir\time_spill`var'_final.dta"
}
